package summit.tests
{
import flash.events.Event;

import summit.basecamp.constants.CommentableType;
import summit.basecamp.helpers.CommentHelper;
import summit.basecamp.helpers.MessageHelper;
import summit.basecamp.helpers.MilestoneHelper;
import summit.basecamp.helpers.TodoItemHelper;
import summit.basecamp.helpers.TodoListHelper;
import summit.data.AccountToken;
import summit.events.BasecampAPIEvent;
import summit.tests.tools.SummitTestContainer;
import summit.tests.tools.TestHelpers;

public class CommentHelperTests extends SummitTestContainer
{
	private var _commentMilestoneId:String;
	private var _commentTodoItemId:String;
	private var _commentMessageId:String;
	private var _todoItemId:String;
	private var _messageId:String;
	private var _milestoneId:String;
	private var _todoListId:String
	
	public function CommentHelperTests(token:AccountToken)
	{
		super(token);
	}
	
	override protected function self_CompleteInit(e:Event):void
	{
		super.initializeLoader(loader_OKInitPrepareTodoList);
		loader.load(TodoListHelper.prepare(projectId));
	}
	
	private function loader_OKInitPrepareTodoList(e:BasecampAPIEvent):void
	{
		super.initializeLoader(loader_OKInitCreateTodoList);
		loader.load(TodoListHelper.create(e.data));
	}
	
	private function loader_OKInitCreateTodoList(e:BasecampAPIEvent):void
	{
		_todoListId = 
		TestHelpers.parseForIdFromResponseHeaders(e.responseHeaders);
		
		super.initializeLoader(loader_OKInitPrepareTodoItem);
		loader.load(TodoItemHelper.prepare(_todoListId));
	}
	
	private function loader_OKInitPrepareTodoItem(e:BasecampAPIEvent):void
	{
		super.initializeLoader(loader_OKInitCreateTodoItem);
		loader.load(TodoItemHelper.create(e.data));
	}
	
	private function loader_OKInitCreateTodoItem(e:BasecampAPIEvent):void
	{
		_todoItemId = 
		TestHelpers.parseForIdFromResponseHeaders(e.responseHeaders);
		
		super.initializeLoader(loader_OKInitPrepareMessage);
		loader.load(MessageHelper.prepare(projectId));
	}
	
	private function loader_OKInitPrepareMessage(e:BasecampAPIEvent):void
	{
		super.initializeLoader(loader_OKInitCreateMessage);
		
		var msgXML:XML = MessageHelperTests.prepareMessageForCreate(e.data);
		
		loader.load(MessageHelper.create(msgXML));
	}
	
	private function loader_OKInitCreateMessage(e:BasecampAPIEvent):void
	{
		_messageId = 
		TestHelpers.parseForIdFromResponseHeaders(e.responseHeaders);
		
		super.initializeLoader(loader_OKInitPrepareMilestone);
		loader.load(MilestoneHelper.prepare(projectId));
	}
	
	private function loader_OKInitPrepareMilestone(e:BasecampAPIEvent):void
	{
		super.initializeLoader(loader_OKInitCreateMilestone);
		loader.load(MilestoneHelper.create(e.data));
	}
	
	private function loader_OKInitCreateMilestone(e:BasecampAPIEvent):void
	{
		_milestoneId  = e.data.milestone[0].id[0];
		
		super.initializeLoader(loader_OKInitPrepareComment);
		loader.load(CommentHelper.prepare(_milestoneId, 
		CommentableType.MILESTONE));
	}
	
	private function loader_OKInitPrepareComment(e:BasecampAPIEvent):void
	{
		super.initializeLoader(loader_OKInitCreateComment);
		loader.load(CommentHelper.create(e.data));
	}
	
	private function loader_OKInitCreateComment(e:BasecampAPIEvent):void
	{
		_commentMilestoneId = 
		TestHelpers.parseForIdFromResponseHeaders(e.responseHeaders);
		
		initializeLoader(loader_OKInitPrepareMsgComment);
		loader.load(CommentHelper.prepare(_messageId, 
		CommentableType.POST)); //super.self_CompleteInit(null);
	}
	
	private function loader_OKInitPrepareMsgComment(e:BasecampAPIEvent):void
	{
		super.initializeLoader(loader_OKInitCreateMsgComment);
		loader.load(CommentHelper.create(e.data));
	}
	
	private function loader_OKInitCreateMsgComment(e:BasecampAPIEvent):void
	{
		_commentMessageId = 
		TestHelpers.parseForIdFromResponseHeaders(e.responseHeaders);
		
		initializeLoader(loader_OKInitPrepareItemComment);
		loader.load(CommentHelper.prepare(_todoItemId, 
		CommentableType.TODOITEM));
	}
	
	private function loader_OKInitPrepareItemComment(e:BasecampAPIEvent):void
	{
		super.initializeLoader(loader_OKInitCreateItemComment);
		loader.load(CommentHelper.create(e.data));
	}
	
	private function loader_OKInitCreateItemComment(e:BasecampAPIEvent):void
	{
		_commentTodoItemId = 
		TestHelpers.parseForIdFromResponseHeaders(e.responseHeaders);
		
		super.self_CompleteInit(null);
	}
	
	override public function cleanUp():void
	{
		super.initializeLoader(loader_OKCleanUpCommentDestroy);
		loader.load(CommentHelper.destroy(_commentMilestoneId));
	}
	
	private function loader_OKCleanUpCommentDestroy(e:BasecampAPIEvent):void
	{
		super.initializeLoader(loader_OKCleanUpMilestoneDestroy);
		loader.load(MilestoneHelper.destroy(_milestoneId));
	}
	
	private function loader_OKCleanUpMilestoneDestroy(e:BasecampAPIEvent):void
	{
		super.initializeLoader(loader_OKCleanUpTodoItemDestroy);
		loader.load(TodoItemHelper.destroy(_todoItemId));
	}
	
	private function loader_OKCleanUpTodoItemDestroy(e:BasecampAPIEvent):void
	{
		super.initializeLoader(loader_OKCleanUpTodoListDestroy);
		loader.load(TodoListHelper.destroy(_todoListId));
	}
	
	private function loader_OKCleanUpTodoListDestroy(e:BasecampAPIEvent):void
	{
		super.initializeLoader(loader_OKCleanUpMessageDestroy);
		loader.load(MessageHelper.destroy(_messageId));
	}
	
	private function loader_OKCleanUpMessageDestroy(e:BasecampAPIEvent):void
	{
		super.cleanUp();
	}
	
	public function readMsgCommentTest(context:Object):void
	{
		initializeLoader(super.loader_OKComplete);
		loader.load(CommentHelper.read(_commentMessageId));
	}
	
	public function readItemCommentTest(context:Object):void
	{
		initializeLoader(super.loader_OKComplete);
		loader.load(CommentHelper.read(_commentTodoItemId));
	}
	
	public function readMilestoneCommentTest(content:Object):void
	{
		initializeLoader(super.loader_OKComplete);
		loader.load(CommentHelper.read(_commentMilestoneId));
	}
	
	public function listMsgTest(content:Object):void
	{
		initializeLoader(super.loader_OKComplete);
		loader.load(CommentHelper.list(_messageId, CommentableType.POST));		
	}
	
	public function listTodoItemTest(content:Object):void
	{
		initializeLoader(super.loader_OKComplete);
		loader.load(CommentHelper.list(_todoItemId, CommentableType.TODOITEM));		
	}
	
	public function listMilestoneTest(content:Object):void
	{
		initializeLoader(super.loader_OKComplete);
		loader.load(CommentHelper.list(_milestoneId, 
		CommentableType.MILESTONE));		
	}
	
	public function editUpdateTest(content:Object):void
	{
		initializeLoader(loader_OKEdit);
		loader.load(CommentHelper.edit(_commentTodoItemId));
	}
	
	private function loader_OKEdit(e:BasecampAPIEvent):void
	{
		initializeLoader(super.loader_OKComplete);
		loader.load(CommentHelper.update(e.data));
	}	
}
}